<template>
  <a-popover
    v-model="visible"
    trigger="click"
    placement="bottomRight"
    overlayClassName="header-notice-wrapper"
    :getPopupContainer="() => $refs.noticeRef.parentElement"
    :autoAdjustOverflow="true"
    :arrowPointAtCenter="true"
    :overlayStyle="{ width: '300px', top: '50px' }"
  >
    <template slot="content">
      <a-spin :spinning="loading">
        <a-tabs>
          <a-tab-pane tab="消息" key="1">
            <a-list>
              <a-list-item>
                <a-list-item-meta :title="title">
                  <a-avatar style="background-color: white" slot="avatar" src="/img/ThXAXghbEsBCCSDihZxY.png"/>
                </a-list-item-meta>
              </a-list-item>
            </a-list>
          </a-tab-pane>
        </a-tabs>
      </a-spin>
    </template>
    <span @click="fetchNotice" class="header-notice" ref="noticeRef" style="padding: 0 18px">
      <a-badge :count="count" :showZero="showZero">
        <a-icon style="font-size: 16px; padding: 4px" type="bell" />
      </a-badge>
    </span>
  </a-popover>
</template>

<script>
export default {
  name: 'HeaderNotice',
  data () {
    return {
      count:0,
      title:'',
      showZero:false,
      loading: false,
      visible: false
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    activated() {
          const _this = this
          _this.myRequest('/getAllNews',{})
            .then(res => {
              const success = res.success
              if(success){
                _this.count = res.result
                _this.title = '你有'+res.result+'条未读消息'
                if(res.result==0){
                   _this.title = '你还没有未读消息'
                }
              }
            })
    },
    init() {
      const _this = this
      _this.myRequest('/getAllNews',{})
        .then(res => {
          const success = res.success
          if(success){
            _this.count = res.result
            _this.title = '你有'+res.result+'条未读消息'
            if(res.result==0){
               _this.title = '你还没有未读消息'
            }
          }
        })
    },
    fetchNotice () {
      if (!this.visible) {
      } else {
        this.loading = false
      }
      this.visible = !this.visible
    }
  }
}
</script>

<style lang="css">
  .header-notice-wrapper {
    top: 50px !important;
  }
</style>
<style lang="less" scoped>
  .header-notice{
    display: inline-block;
    transition: all 0.3s;

    span {
      vertical-align: initial;
    }
  }
</style>
